package com.example.javagd.util;

import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * @program: testjson
 * @description:
 * @author: hzy
 * @create: 2021-02-10 20:05
 **/
@Component
public class JsonFileUtils {

    @Autowired
    Gson gson;

    private String jsonFile;

    public Object ReadFromFile(){
        Object jsonResult = new Object();
        try {

            File file = new File(jsonFile);
            if (!file.exists()){
                file.createNewFile();
            }

            FileReader reader = new FileReader(jsonFile);
            jsonResult = gson.fromJson(reader, Object.class);
            reader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonResult;
    }

    public void WriteToFile(Object object){
        try {
            FileWriter writer = new FileWriter(jsonFile);
            gson.toJson(object, writer);
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void Add(){

    }

    public void Delete(){

    }

    public Object Select(){
        return null;
    }


}
